#!/usr/bin/python3
# -*- coding:utf-8 -*-
"""
@author: KevinQi
@file: permission.py
@time: 2023/12/25 17:50
@desc:
"""
from sqlalchemy.orm import relationship
from backend.conf import config
from backend.workjob.utils.model_utils import ModelBase
from backend.workjob.utils.mysql_base import SqlAlBase, MysqlBase
from sqlalchemy import Column, Integer, String, ForeignKey, Table, BigInteger, Text


class Dept(ModelBase, SqlAlBase.base):
    """部门表"""
    __tablename__ = config.table_prefix + 'system_dept'
    id = Column(BigInteger, primary_key=True, comment="Id")
    name = Column(String(64), nullable=False, unique=False, comment="部门名称")
    key = Column(String(64), nullable=True, unique=False, comment="关联字符")
    sort = Column(Integer, nullable=False, unique=False, comment="排序")
    phone = Column(String(64), nullable=True, unique=False, comment="电话")
    status = Column(Integer, nullable=False, unique=False, default=1, comment="状态 1  0 关")
    parent_id = Column(BigInteger, ForeignKey(config.table_prefix + 'system_dept' + '.id'))
    parent = relationship("Dept", backref="child_depts", remote_side=[id], foreign_keys=[parent_id])
    manager_id = Column(BigInteger, ForeignKey(config.table_prefix + 'system_user' + '.id'))
    manager = relationship("User", backref="manage_dept", uselist=False, foreign_keys=[manager_id])


class User(ModelBase, SqlAlBase.base):
    """用户表"""
    __tablename__ = config.table_prefix + 'system_user'
    work_num = Column(String(64), nullable=False, unique=True, comment="工号")
    email = Column(String(64), nullable=True, unique=False, comment="邮箱")
    avatar = Column(String(255), nullable=True, unique=False, comment="头像")
    name = Column(String(64), nullable=False, unique=False, comment="现实名字")
    gender = Column(Integer, nullable=False, unique=False, comment="性别 0男 1女")
    passwd = Column(String(255), nullable=False, unique=False, default=0, comment="密码")
    super_admin = Column(Integer, nullable=False, unique=False, default=0, comment="是否为超级管理员")
    last_token = Column(String(255), nullable=True, unique=False, comment="最后一次登录Token")
    dept_id = Column(BigInteger, ForeignKey(config.table_prefix + 'system_dept' + '.id'))
    dept = relationship("Dept", backref="users", foreign_keys=[dept_id])
    roles = relationship("Role", secondary=config.table_prefix + "system_user_role")


# 角色表
class Role(ModelBase, SqlAlBase.base):
    """角色表"""
    __tablename__ = config.table_prefix + 'system_role'
    name = Column(String(64), nullable=False, unique=False, default=1, comment="角色名称")
    role_key = Column(String(64), nullable=False, unique=False, default="", comment="权限标识")
    status = Column(Integer, nullable=False, unique=False, default=1, comment="角色状态 1开始 0关闭")
    remark = Column(String(64), nullable=True, unique=False, default="", comment="备注")
    is_admin = Column(Integer, nullable=False, unique=False, default=0, comment="是否超级管理员 0不是1是")
    menu = relationship("Menu", secondary=config.table_prefix + "system_role_menu")


# 权限表
class Menu(ModelBase, SqlAlBase.base):
    """菜单表"""
    __tablename__ = config.table_prefix + 'system_menu'
    id = Column(BigInteger, primary_key=True, comment="Id")
    name = Column(String(64), nullable=False, unique=False, default=1, comment="菜单名称")
    icon = Column(String(64), nullable=True, unique=False, default=1, comment="icon")
    sort = Column(Integer, nullable=False, unique=False, default=1, comment="排序")
    is_link = Column(Integer, nullable=True, unique=False, default=0, comment="是否外链")
    is_catalog = Column(Integer, nullable=False, unique=False, default=1, comment="0 目录 1菜单 2按钮")
    web_path = Column(String(64), nullable=True, unique=False, default="", comment="路由地址")
    component = Column(String(64), nullable=True, unique=False, default="", comment="组件地址")
    component_name = Column(String(64), nullable=True, unique=False, default="", comment="前端组件名称")
    interface_path = Column(String(256), nullable=True, unique=False, default="", comment="后端接口地址")
    interface_method = Column(String(32), nullable=True, unique=False, default="", comment="接口请求方式 GET POST")
    status = Column(Integer, nullable=False, unique=False, default=1, comment="菜单状态 0关闭 1开启")
    cache = Column(Integer, nullable=True, unique=False, default=1, comment="是否页面缓存")
    perms = Column(String(256), nullable=True, unique=False, default=1, comment="权限标识")
    visible = Column(Integer, nullable=False, unique=False, default=1, comment="侧边栏中是否显示0关闭 1显示")
    parent_id = Column(BigInteger, ForeignKey(config.table_prefix + 'system_menu' + '.id'))
    parent = relationship("Menu", backref="child_menus", remote_side=[id], foreign_keys=[parent_id])


class Post(ModelBase, SqlAlBase.base):
    """岗位表"""
    __tablename__ = config.table_prefix + 'system_post'
    name = Column(String(64), nullable=False, unique=False, default=1, comment="岗位名称")
    post_key = Column(String(64), nullable=False, unique=False, default="", comment="岗位权限标识")
    sort = Column(Integer, nullable=False, unique=False, default=1, comment="排序")
    remark = Column(String(64), nullable=True, unique=False, default="", comment="备注")


class File(ModelBase, SqlAlBase.base):
    """文件管理"""
    __tablename__ = config.table_prefix + 'system_file'
    name = Column(String(64), nullable=False, unique=False, default="", comment="文件名称")
    key = Column(String(256), nullable=False, unique=False, default="", comment="岗位权限标识")
    tag = Column(String(32), nullable=False, unique=False, default="", comment="文件类型")
    url = Column(Text, nullable=False, unique=False, default="", comment="文件地址")
    remark = Column(String(64), nullable=True, unique=False, default="", comment="备注")


# 用户-角色关联表
user_role = Table(config.table_prefix + 'system_user_role', SqlAlBase().base.metadata,
                  Column('user_id', BigInteger, ForeignKey(config.table_prefix + 'system_user' + '.id')),
                  Column('role_id', BigInteger, ForeignKey(config.table_prefix + 'system_role' + '.id'))
                  )

# 角色-权限关联表
role_menu = Table(config.table_prefix + 'system_role_menu', SqlAlBase().base.metadata,
                  Column('role_id', BigInteger, ForeignKey(config.table_prefix + 'system_role' + '.id')),
                  Column('menu_id', BigInteger, ForeignKey(config.table_prefix + 'system_menu' + '.id'))
                  )


class ConfigSettings(ModelBase, SqlAlBase.base):
    """参数设置"""
    __tablename__ = config.table_prefix + 'system_configsettings'
    name = Column(String(64), nullable=False, unique=False, default="", comment="参数名称")
    key = Column(String(256), nullable=False, unique=False, default="", comment="参数键名")
    value = Column(String(256), nullable=False, unique=False, default="", comment="参数键值")
    is_system = Column(Integer, nullable=False, unique=False, default=0, comment="是否内置 0 否 1是")
    status = Column(Integer, nullable=False, unique=False, default=1, comment="状态 0 关 1开")
    remark = Column(String(256), nullable=True, unique=False, default="", comment="备注")


class DictData(ModelBase, SqlAlBase.base):
    """字典管理"""
    __tablename__ = config.table_prefix + 'system_dict_data'
    name = Column(String(64), nullable=True, unique=False, default="", comment="字典名称")
    dict_type = Column(String(256), nullable=True, unique=False, default="", comment="字典类型")
    status = Column(Integer, nullable=True, unique=False, default=1, comment="状态 0 关 1开")
    remark = Column(String(256), nullable=True, unique=False, default="", comment="备注")


class DictDataDetails(ModelBase, SqlAlBase.base):
    """字典详情"""
    __tablename__ = config.table_prefix + 'system_dict_data_details'
    lable = Column(String(64), nullable=True, unique=False, default="", comment="字典标签")
    value = Column(String(64), nullable=True, unique=False, default="", comment="字典键值")
    is_system = Column(Integer, nullable=True, unique=False, default=1, comment="是否系统 0否 1是")
    status = Column(Integer, nullable=True, unique=False, default=1, comment="状态 0 关 1开")
    sort = Column(Integer, nullable=True, unique=False, default=1, comment="排序")
    dict_id = Column(BigInteger, ForeignKey(config.table_prefix + 'system_dict_data' + '.id'), comment="关联字典")
    dict = relationship("DictData", backref="dict_data", foreign_keys=[dict_id])
    remark = Column(String(256), nullable=True, unique=False, default="", comment="备注")
